package atmGUI;

import util.Util;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.List;

//修改密码界面
public class ChangePwdPanel extends JPanel implements ActionListener {
    private Icon changePwdImage = Util.getIcon("/image/buttonModify.jpg");//改密按钮图
    private Icon backImage = Util.getIcon("/image/pane.jpg");//返回按钮图
    private ATM_GUI_Main frame;//顶级容器实例
    private List<User> usersList;//用户信息集合实例
    private JButton backBtn;//返回按钮
    private JLabel jLabelBack;//返回文本
    private JLabel oldPwdJLabel;//旧密码提示插件
    private JTextField iptOldPwd;//旧密码输入框
    private JLabel newPwdJLabel;//新密码提示插件
    private JTextField iptNewPwd;//新密码输入框
    private JLabel confirmPwdJLabel;//确认新密码提示插件
    private JTextField iptConfirmPwd;//确认新密码输入框
    private JButton changePwdBtn;//改密按钮

    public ChangePwdPanel(ATM_GUI_Main frame) {
        this.frame = frame;
        //设置空布局和透明度
        this.setOpaque(false);
        this.setLayout(null);
        //旧密码提示插件
        oldPwdJLabel = new JLabel("请输入旧密码");
        oldPwdJLabel.setBounds(240, 190, 150, 50);
        oldPwdJLabel.setFont(new Font("宋体", Font.PLAIN, 20));
        this.add(oldPwdJLabel);
        //旧密码输入框
        iptOldPwd = new JTextField();
        iptOldPwd.setBounds(420, 200, 300, 30);
        iptOldPwd.setFont(new Font("宋体", Font.PLAIN, 20));
        this.add(iptOldPwd);
        //新密码提示插件
        newPwdJLabel = new JLabel("请输入新密码");
        newPwdJLabel.setBounds(240, 230, 150, 50);
        newPwdJLabel.setFont(new Font("宋体", Font.PLAIN, 20));
        this.add(newPwdJLabel);
        //新密码输入框
        iptNewPwd = new JTextField();
        iptNewPwd.setBounds(420, 240, 300, 30);
        iptNewPwd.setFont(new Font("宋体", Font.PLAIN, 20));
        this.add(iptNewPwd);
        //确认新密码提示插件
        confirmPwdJLabel = new JLabel("请再次输入新密码");
        confirmPwdJLabel.setBounds(240, 270, 200, 50);
        confirmPwdJLabel.setFont(new Font("宋体", Font.PLAIN, 20));
        this.add(confirmPwdJLabel);
        //确认新密码输入框
        iptConfirmPwd = new JTextField();
        iptConfirmPwd.setBounds(420, 280, 300, 30);
        iptConfirmPwd.setFont(new Font("宋体", Font.PLAIN, 20));
        this.add(iptConfirmPwd);
        //改密码按钮
        changePwdBtn = new JButton(changePwdImage);
        changePwdBtn.setBounds(870, 220, 140, 50);
        this.add(changePwdBtn);
        changePwdBtn.addActionListener(this);

        //返回按钮
        jLabelBack = new JLabel("返  回");
        jLabelBack.setBounds(900, 300, 100, 50);
        jLabelBack.setForeground(Color.white);
        this.add(jLabelBack);
        jLabelBack.setFont(new Font("宋体", Font.BOLD, 25));
        backBtn = new JButton(backImage);
        backBtn.setBounds(870, 300, 140, 50);
        backBtn.setOpaque(false);
        this.add(backBtn);
        backBtn.addActionListener(this);//建立联系
    }

    @Override
    public void actionPerformed(ActionEvent e) {
        if (e.getSource() == backBtn) {
            iptOldPwd.setText("");
            iptNewPwd.setText("");
            iptConfirmPwd.setText("");
            frame.switchPanel("menu");
        } else if (e.getSource() == changePwdBtn) {
            String loginOldPwd = String.valueOf(frame.getLoginUser().getPassword());//原有的密码
            boolean bl = iptNewPwd.getText().matches("^(?=.*\\d)(?=.*[a-z])(?=.*[A-Z]).{8,10}$");
            if (loginOldPwd.equals(iptOldPwd.getText())) {
                if (!bl) {
                    JOptionPane.showMessageDialog(this, "密码格式错误");
                } else if (iptOldPwd.getText().equals(iptNewPwd.getText())) {
                    JOptionPane.showMessageDialog(this, "新密码不能和旧密码相同,请重新输入");
                } else if (!iptNewPwd.getText().equals(iptConfirmPwd.getText())) {
                    JOptionPane.showMessageDialog(this, "新密码和确认密码不一致,请重新输入");
                } else {
                    frame.getLoginUser().setPassword(iptConfirmPwd.getText());
                    JOptionPane.showMessageDialog(this, "修改密码成功，请重新登录，新密码为：" + iptConfirmPwd.getText());
                    iptOldPwd.setText("");
                    iptNewPwd.setText("");
                    iptConfirmPwd.setText("");
                    frame.getLoginPanel().getIptPassword().setText("");
                    frame.switchPanel("login");
                }
            } else {
                JOptionPane.showMessageDialog(this, "旧密码不匹配,请重新输入");
            }

        }
    }
}
